package com.industrial.premu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuIndexWeightMain;
import com.industrial.premu.dto.IndexWeightParamsDto;
import com.industrial.premu.service.IPreMuIndexWeightMainService;
import com.industrial.premu.vo.PreMuIndexWeightMainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuCustomIndicator;
import com.industrial.premu.service.IPreMuCustomIndicatorService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 亩产自定义指标Controller
 *
 * @author ZhouS
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/indicator")
public class PreMuCustomIndicatorController extends BaseController
{
    @Autowired
    private IPreMuCustomIndicatorService preMuCustomIndicatorService;
    @Autowired
    private IPreMuIndexWeightMainService preMuIndexWeightMainService;
    /**
     * 查询亩产自定义指标列表
     */
    @RequiresPermissions("premu:indicator:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuCustomIndicator preMuCustomIndicator)
    {
        startPage();
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuCustomIndicator.setCounty(district);
        List<PreMuCustomIndicator> list = preMuCustomIndicatorService.selectPreMuCustomIndicatorList(preMuCustomIndicator);
        return getDataTable(list);
    }

    /**
     * 导出亩产自定义指标列表
     */
    @RequiresPermissions("premu:indicator:export")
    @Log(title = "亩产自定义指标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuCustomIndicator preMuCustomIndicator)
    {

        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        preMuCustomIndicator.setCounty(district);
        List<PreMuCustomIndicator> list = preMuCustomIndicatorService.selectPreMuCustomIndicatorList(preMuCustomIndicator);
        ExcelUtil<PreMuCustomIndicator> util = new ExcelUtil<PreMuCustomIndicator>(PreMuCustomIndicator.class);
        util.exportExcel(response, list, "亩产自定义指标数据");
    }

    /**
     * 获取亩产自定义指标详细信息
     */
    @RequiresPermissions("premu:indicator:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(preMuCustomIndicatorService.selectPreMuCustomIndicatorById(id));
    }

    /**
     * 新增亩产自定义指标
     */
    @RequiresPermissions("premu:indicator:add")
    @Log(title = "亩产自定义指标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuCustomIndicator preMuCustomIndicator)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();

        if(StringUtils.isNotNull(preMuCustomIndicator.getCompanyType())&&StringUtils.isNotNull(preMuCustomIndicator.getYear())){
            IndexWeightParamsDto indexWeightParams =new IndexWeightParamsDto();
            indexWeightParams.setCounty(district);
            indexWeightParams.setCompanyType(preMuCustomIndicator.getCompanyType());
            indexWeightParams.setYear(preMuCustomIndicator.getYear());
            indexWeightParams.setIndexName("自定义指标");
//            PreMuIndexWeightMainVo preMuIndexWeightMainVo=   preMuIndexWeightMainService.selecPreMuIndexWeightMain(indexWeightParams);
//            double indexValue=0;
//            if(preMuIndexWeightMainVo!=null){
//                if(StringUtils.isNotNull(preMuIndexWeightMainVo.getIndexValue())){
//                    indexValue=Double.valueOf(preMuIndexWeightMainVo.getIndexValue());
//                }
//            }
            List<PreMuIndexWeightMainVo> weightList=   preMuIndexWeightMainService.weightList(indexWeightParams);
            double indexValue=0;
            if(CollectionUtils.isNotEmpty(weightList)){
                if(StringUtils.isNotNull(weightList.get(0).getIndexValue())){
                    indexValue=Double.valueOf(weightList.get(0).getIndexValue());
                }
            }
        if(Double.valueOf(preMuCustomIndicator.getIndicatorValues())>indexValue){
            return warn("自定义指标分值不得大于指标管理中自定义指标分数！");
        }else{

            PreMuCustomIndicator preMuCustomIndicatorNew=new PreMuCustomIndicator();
            preMuCustomIndicatorNew.setCounty(district);
            preMuCustomIndicatorNew.setCompanyType(preMuCustomIndicator.getCompanyType());
            preMuCustomIndicatorNew.setYear(preMuCustomIndicator.getYear());
            List<PreMuCustomIndicator> list = preMuCustomIndicatorService.selectPreMuCustomIndicatorList(preMuCustomIndicatorNew);
            double totle=0;
            for (int i = 0; i <list.size() ; i++) {
                totle+=Double.valueOf(list.get(i).getIndicatorValues());
                if(list.get(i).getName().contains(preMuCustomIndicator.getName())){
                    return warn("已设置过相同的自定义指标！");
                }
            }
            if(totle+Double.valueOf(preMuCustomIndicator.getIndicatorValues())>indexValue){
                return warn("自定义指标分值不得大于指标管理中自定义指标分数！");
            }else{
                return toAjax(preMuCustomIndicatorService.insertPreMuCustomIndicator(preMuCustomIndicator));
            }

        } }else{
            return  warn("选择的数据不全！");
        }

    }


    /**
     * 修改亩产自定义指标
     */
    @RequiresPermissions("premu:indicator:edit")
    @Log(title = "亩产自定义指标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuCustomIndicator preMuCustomIndicator)
    {
         String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();

        if(StringUtils.isNotNull(preMuCustomIndicator.getCompanyType())&&StringUtils.isNotNull(preMuCustomIndicator.getYear())){
             IndexWeightParamsDto indexWeightParams =new IndexWeightParamsDto();
            indexWeightParams.setCounty(district);
            indexWeightParams.setCompanyType(preMuCustomIndicator.getCompanyType());
            indexWeightParams.setYear(preMuCustomIndicator.getYear());
            indexWeightParams.setIndexName("自定义指标");
//            PreMuIndexWeightMainVo preMuIndexWeightMainVo=   preMuIndexWeightMainService.selecPreMuIndexWeightMain(indexWeightParams);
//            double indexValue=0;
//            if(preMuIndexWeightMainVo!=null){
//                if(StringUtils.isNotNull(preMuIndexWeightMainVo.getIndexValue())){
//                    indexValue=Double.valueOf(preMuIndexWeightMainVo.getIndexValue());
//                }
//            }
            List<PreMuIndexWeightMainVo> weightList=   preMuIndexWeightMainService.weightList(indexWeightParams);
            double indexValue=0;
            if(CollectionUtils.isNotEmpty(weightList)){
                if(StringUtils.isNotNull(weightList.get(0).getIndexValue())){
                    indexValue=Double.valueOf(weightList.get(0).getIndexValue());
                }
            }
            if(Double.valueOf(preMuCustomIndicator.getIndicatorValues())>indexValue){
                return warn("自定义指标分值不得大于指标管理中自定义指标分数！");
            }else {
                PreMuCustomIndicator preMuCustomIndicatorNew=new PreMuCustomIndicator();
                preMuCustomIndicatorNew.setCounty(district);
                preMuCustomIndicatorNew.setCompanyType(preMuCustomIndicator.getCompanyType());
                preMuCustomIndicatorNew.setYear(preMuCustomIndicator.getYear());
                List<PreMuCustomIndicator> list = preMuCustomIndicatorService.selectPreMuCustomIndicatorList(preMuCustomIndicatorNew);
                double totle=0;
                for (int i = 0; i <list.size() ; i++) {
                    if(!preMuCustomIndicator.getId().equals(list.get(i).getId())){
                        totle+=Double.valueOf(list.get(i).getIndicatorValues());
                        if(list.get(i).getName().contains(preMuCustomIndicator.getName())){
                            return warn("已设置过相同的自定义指标！");
                        }
                    }
                }
                if(totle+Double.valueOf(preMuCustomIndicator.getIndicatorValues())>indexValue){
                    return warn("自定义指标分值不得大于指标管理中自定义指标分数！");
                }else{
                    return toAjax(preMuCustomIndicatorService.updatePreMuCustomIndicator(preMuCustomIndicator));
                }
            }
        }else{
            return  warn("选择的数据不全！");
        }
    }

    /**
     * 删除亩产自定义指标
     */
    @RequiresPermissions("premu:indicator:remove")
    @Log(title = "亩产自定义指标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(preMuCustomIndicatorService.deletePreMuCustomIndicatorByIds(ids));
    }


}
